package com.nontax.test;

import com.alibaba.fastjson.JSON;
import com.nontax.test.common.BaseController;
import com.nontax.test.common.domain.BaseRequest;
import com.nontax.test.common.domain.VoidResponse;
import com.nontax.test.common.exception.BusinessException;
import com.nontax.test.common.websocket.WebSocketServer;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import static com.nontax.test.util.OldRestCilent.callOldInterface;
import static com.nontax.test.util.StdRestClient.callStdInterface;

@Slf4j
@Controller
@RequestMapping("/common")
public class SendCommonController extends BaseController {

    /**
     * 发送通用请求
     */
    @ResponseBody
    @PostMapping("/sendCommon")
    public void commonSend(HttpServletRequest request,BaseRequest baseRequest,String jsonData) throws IOException {
        WebSocketServer.sendInfo("前端参数为:"+JSON.toJSONString(baseRequest),getSid());
        String url = generateRequest(baseRequest, "");
        if(StringUtils.isNotEmpty(jsonData)){
            //直接发送json数据
            Map requestMap = JSON.parseObject(jsonData, Map.class);
            log.info("发送的数据为:{}",requestMap);
            try {
                if(StringUtils.equals(baseRequest.getUseStdRequest(),"1")){
                    callStdInterface(requestMap,VoidResponse.class, url, baseRequest.getAppId(), baseRequest.getAppKey(),getSid());
                }else{
                    callOldInterface(url,requestMap,baseRequest.getToken(),getSid());
                }
            } catch (Exception e) {
                throw new BusinessException(e.getMessage());
            }
        }else{
            //发送手动添加的数据
            Map<String, String[]> parameterMap = request.getParameterMap();
            Map<String,String> rquestMap = new HashMap<>();
            Set<String> keys = parameterMap.keySet();
            for(String key:keys){
                Class<? extends BaseRequest> clazz = baseRequest.getClass();
                Field[] fields = clazz.getDeclaredFields();
                for(Field field : fields){
                    if(!StringUtils.equals(field.getName(),key)){
                        rquestMap.put(key,parameterMap.get(key)[0]);
                    }
                }
            }
            try {
                log.info("发送的数据为:{}",rquestMap);
                if(StringUtils.equals(baseRequest.getUseStdRequest(),"1")){
                    callStdInterface(rquestMap,VoidResponse.class, url, baseRequest.getAppId(), baseRequest.getAppKey(),getSid());
                }else{
                    callOldInterface(url,rquestMap,baseRequest.getToken(),getSid());
                }
            } catch (Exception e) {
                throw new BusinessException(e.getMessage());
            }
        }
    }
}